package org.fungene.services.db;

import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashMap;

import javax.xml.namespace.QName;

import org.codehaus.xfire.XFireRuntimeException;
import org.codehaus.xfire.aegis.AegisBindingProvider;
import org.codehaus.xfire.annotations.AnnotationServiceFactory;
import org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.jaxb2.JaxbTypeRegistry;
import org.codehaus.xfire.service.Endpoint;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.soap.AbstractSoapBinding;
import org.codehaus.xfire.transport.TransportManager;

public class AccessBioDBClient {

	private static XFireProxyFactory proxyFactory = new XFireProxyFactory();
	private HashMap endpoints = new HashMap();
	private Service service0;

	public AccessBioDBClient() {

		// Properties prop = PrptyReader.loadPrpty("dbServic.properties");
		create0();
		Endpoint AccessBioDBPortTypeLocalEndpointEP = service0.addEndpoint(
				new QName("http://myservices",
						"AccessBioDBPortTypeLocalEndpoint"),
				new QName("http://myservices",
						"AccessBioDBPortTypeLocalBinding"),
				"xfire.local://AccessBioDB");
		endpoints.put(new QName("http://myservices",
				"AccessBioDBPortTypeLocalEndpoint"),
				AccessBioDBPortTypeLocalEndpointEP);
		Endpoint AccessBioDBHttpPortEP = service0.addEndpoint(new QName(
				"http://myservices", "AccessBioDBHttpPort"), new QName(
				"http://myservices", "AccessBioDBHttpBinding"), "http://"
				+ "localhost:8080"// prop.getProperty("ServiceIP")
				+ "/AccessBioDB/services/AccessBioDB");
		endpoints.put(new QName("http://myservices", "AccessBioDBHttpPort"),
				AccessBioDBHttpPortEP);
	}

	public Object getEndpoint(Endpoint endpoint) {
		try {
			return proxyFactory.create((endpoint).getBinding(), (endpoint)
					.getUrl());
		} catch (MalformedURLException e) {
			throw new XFireRuntimeException("Invalid URL", e);
		}
	}

	public Object getEndpoint(QName name) {
		Endpoint endpoint = ((Endpoint) endpoints.get((name)));
		if ((endpoint) == null) {
			throw new IllegalStateException("No such endpoint!");
		}
		return getEndpoint((endpoint));
	}

	public Collection getEndpoints() {
		return endpoints.values();
	}

	private void create0() {
		TransportManager tm = (org.codehaus.xfire.XFireFactory.newInstance()
				.getXFire().getTransportManager());
		HashMap props = new HashMap();
		props.put("annotations.allow.interface", true);
		AnnotationServiceFactory asf = new AnnotationServiceFactory(
				new Jsr181WebAnnotations(), tm, new AegisBindingProvider(
						new JaxbTypeRegistry()));
		asf.setBindingCreationEnabled(false);
		service0 = asf.create(
				(org.fungene.services.db.AccessBioDBPortType.class), props);
		{
			AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0,
					new QName("http://myservices",
							"AccessBioDBPortTypeLocalBinding"),
					"urn:xfire:transport:local");
		}
		{
			AbstractSoapBinding soapBinding = asf.createSoap11Binding(service0,
					new QName("http://myservices", "AccessBioDBHttpBinding"),
					"http://schemas.xmlsoap.org/soap/http");
		}
	}

	public AccessBioDBPortType getAccessBioDBPortTypeLocalEndpoint() {
		return ((AccessBioDBPortType) (this).getEndpoint(new QName(
				"http://myservices", "AccessBioDBPortTypeLocalEndpoint")));
	}

	public AccessBioDBPortType getAccessBioDBPortTypeLocalEndpoint(String url) {
		AccessBioDBPortType var = getAccessBioDBPortTypeLocalEndpoint();
		org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
		return var;
	}

	public AccessBioDBPortType getAccessBioDBHttpPort() {
		return ((AccessBioDBPortType) (this).getEndpoint(new QName(
				"http://myservices", "AccessBioDBHttpPort")));
	}

	public AccessBioDBPortType getAccessBioDBHttpPort(String url) {
		AccessBioDBPortType var = getAccessBioDBHttpPort();
		org.codehaus.xfire.client.Client.getInstance(var).setUrl(url);
		return var;
	}

	public static void main(String[] args) {

		AccessBioDBClient client = new AccessBioDBClient();

		// create a default service endpoint
		AccessBioDBPortType service = client.getAccessBioDBHttpPort();

		// TODO: Add custom client code here
		//
		// service.yourServiceOperationHere();
		System.out.println("test client completed");
		System.exit(0);
	}

}
